<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">台账信息单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="物料" prop="materialId">
              <el-select v-model="ruleForm.materialId" :disabled="title === '编辑'" style="width: 100%" @change="ruleForm.unit = materialList.find(item => item.id === ruleForm.materialId).unit">
                <el-option v-for="item in materialList" :key="item.id" :label="`${item.materialName} ${item.unit ? '【' + item.unit + '】' : ''}`" :value="item.id">
                  <span style="float: left">{{ item.materialName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.unit }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="库存数量" prop="num">
              <el-input-number v-model="ruleForm.num" :disabled="title === '编辑'" style="width: 100%" :precision="0" :min="title === '编辑' ? Number(ruleForm.num) : 1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="ruleForm.unit" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="库房名称" prop="wmsId">
              <tree-select ref="treeSelect" style="width: 100%" :props="{
                  value: 'id',
                  label: 'wmsName',
                  children: 'children'
                }" :options="wmsListAll" :accordion="true" :value="ruleForm.wmsId" search-key="wmsName" :disabled="title === '编辑'" @getNode="getValue($event)" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="入库批号" prop="batch">
              <el-input v-model="ruleForm.batch" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="单价" prop="price">
              <el-input-number v-model="ruleForm.price" style="width: 100%" :max="99999999" :precision="2" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="remark">
              <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  props: {
    wmsListAll: {
      type: Array,
      default: () => {
        return {}
      }
    },
    materialList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      title: '',
      ruleForm: {
        batch: '',
        price: '',
        num: '',
        wmsId: '', // 库房库位
        materialId: '',
        remark: '' // 描述
      },
      rules: {
        materialId: [
          { required: true, message: '物料不能为空！', trigger: 'change' }
        ],
        wmsId: [
          { required: true, message: '库房库位不能为空！', trigger: 'change' }
        ],
        num: [
          { required: true, message: '库存数量不能为空！', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
      this.dialogVisible = true
    },
    getValue(value) {
      if (value) {
        this.ruleForm.wmsId = value.id
      } else {
        this.ruleForm.wmsId = ''
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let url = ''
          if (this.title === '新增') {
            url = '/wms/inventory/save'
          } else {
            url = '/wms/inventory/update'
          }
          const res = await this.$axios.post(url, this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = Object.assign({}, this.$options.data.call(this).ruleForm)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
